package cn.yunbamboos.rest.service.apidoc.meta;

import cn.yunbamboos.rest.service.apidoc.IMeta;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Operation implements IMeta {

    private final List<String> tags = new ArrayList<>();
    private String summary;
    private String description;
    private final List<Parameter> parameters = new ArrayList<>();
    private final RequestBody requestBody = new RequestBody();
    private final List<Response> responses = new ArrayList<>();

    @Override
    public JSONObject toJSON() {
        JSONObject json = new JSONObject();
        json.put("tags", tags);
        json.put("summary", summary);
        json.put("description", description);
        // 参数
        JSONArray parameters = new JSONArray();
        for (Parameter parameter : this.parameters) {
            parameters.add(parameter.toJSON());
        }
        json.put("parameters", parameters);
        // 参数body
        json.put("requestBody", requestBody.toJSON());
        // 响应
        JSONObject responses = new JSONObject();
        for (Response response : this.responses) {
            responses.put(response.getCode(), response.toJSON());
        }
        json.put("responses", responses);
        return json;
    }

    public void addTag(String tag) {
        this.tags.add(tag);
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void addParameter(Parameter parameter) {
        this.parameters.add(parameter);
    }

    public void addParameters(List<Parameter> parameters) {
        this.parameters.addAll(parameters);
    }

    public RequestBody getRequestBody() {
        return requestBody;
    }

    public void addResponse(Response response) {
        this.responses.add(response);
    }
}
